﻿namespace HelperLibrary;

/// <summary>
/// DES 加密解密
/// </summary>
public class DESEncrypt
{
    /// <summary>
    /// DES 加密 向量
    /// </summary>
    private static readonly byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

    /// <summary>
    /// DES 加密 向量
    /// </summary>
    private static readonly string KeyStr = "DECRYKEY";

    /// <summary>
    /// DES 加密
    /// </summary>
    /// <param name="encryptString">需要加密的字符串</param>
    /// <param name="encryptKey">加密密钥（8位）</param>
    /// <returns></returns>
    public static string EncryptDes(string encryptString, string encryptKey)
    {
        try
        {
            byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey);
            byte[] rgbIV = Keys;
            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            var des = DES.Create();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, des.CreateEncryptor(rgbKey, rgbIV),
                CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Convert.ToBase64String(mStream.ToArray());
        }
        catch
        {
            return encryptString;
        }
    }

    /// <summary>
    /// DES 解密
    /// </summary>
    /// <param name="decryptString">需要解密的字符串</param>
    /// <param name="decryptKey">解密密钥（8位）</param>
    /// <returns></returns>
    public static string DecryptDes(string decryptString, string decryptKey)
    {
        try
        {
            byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
            byte[] rgbIV = Keys;
            byte[] inputByteArray = Convert.FromBase64String(decryptString);
            var des = DES.Create();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, des.CreateDecryptor(rgbKey, rgbIV),
                CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Encoding.UTF8.GetString(mStream.ToArray());
        }
        catch
        {
            return decryptString;
        }
    }

    /// <summary>
    /// 加密
    /// </summary>
    /// <param name="encryptString"></param>
    /// <param name="encryptKey"></param>
    /// <returns></returns>
    public static string EncryptKeyDes(string encryptString, string encryptKey)
    {
        try
        {
            byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey);
            byte[] rgbIV = Encoding.UTF8.GetBytes(KeyStr);
            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            var des = DES.Create();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, des.CreateEncryptor(rgbKey, rgbIV),
                CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Convert.ToBase64String(mStream.ToArray());
        }
        catch
        {
            return encryptString;
        }
    }


}
